package gff.actions;

import gff.bsn.CatalogosBsn;
import gff.bsn.LoginBsn;
import gff.bsn.UnidadesBsn;
import gff.constantes.Constantes;
import gff.forms.LoginForm;
import gff.vo.LoginVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class LoginAction extends DispatchAction {
	
	
	private static final Logger log = Logger.getLogger(LoginAction.class);

	
	public ActionForward inicio(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		log.info("LoginAction Metodo: inicio ");
		ActionForward forward = new ActionForward();

		request.getSession().removeAttribute("login");
		
		form.reset(mapping, request);

		//
		// LoginBsn.getUnidades();
		//
		// request.getSession().setAttribute("listUnidades",
		// LoginBsn.getUnidades());

		forward = mapping.findForward("recetaAuto");

		return forward;
	}

	public ActionForward validaUsuario(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		log.info("LoginAction Metodo: validaUsuario ");
		
		ActionForward forward = new ActionForward();

		String ruta = "login";
		
		LoginVO login = new LoginVO();
		
		LoginForm loginForm =( LoginForm ) form;
		
		
		login.setUsuario(loginForm.getUsuario());
		login.setPass(loginForm.getPass());
		
		request.getSession().removeAttribute("login");
		
		 LoginVO loginList =LoginBsn.getLogin(login);
		
		if (loginList != null && loginList.getPase().equals("go") ){
					

			ruta = "inicio";
			
			Constantes.getInsumos().clear();
			Constantes.getInsumosList().clear();
			
			Constantes.setInsumos(CatalogosBsn.getInsumos());
			Constantes.setInsumosMap(CatalogosBsn.getInsumosMap());
			Constantes.setFuentes(CatalogosBsn.getFuentes());
			Constantes.setUnidadesMap(CatalogosBsn.getUnidadesMap());
			CatalogosBsn.getParametros();
	
		
			log.info("Se obtiene informacion de Insumos [" + Constantes.getInsumos().size() + "]");
			log.info("Se obtiene informacion de InsumosMap [" + Constantes.getInsumosMap().size() + "]");
			log.info("Se obtiene informacion de fuente [" + Constantes.getFuentes().size() + "]");
			log.info("Se obtiene informacion de Unidades [" + Constantes.getUnidadesMap().size() + "]");
			
			
		
			
			request.getSession().setAttribute("usuarioVO", loginList);
			
			request.getSession().setAttribute("unidadesVO",  UnidadesBsn.getUnidades() );
			
			
			
			
			

		}else{
			
			loginForm.setPase("El usuario o el password ingresado no son validos favor de verificar");
			
		}
		
		
		forward = mapping.findForward(ruta);

		return forward;
	}

	public ActionForward menuReglas(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		log.info("LoginAction Metodo: menuReglas ");
		
		ActionForward forward = new ActionForward();

		String ruta = "login";
		
		LoginVO login = (LoginVO) request.getSession().getAttribute("usuarioVO");
		
		request.getSession().setAttribute("unidadesVORegla",  UnidadesBsn.getUnidadesReglas() );
		
		request.getSession().removeAttribute("reglasUnidad");

		if (login != null && login.getPase().equals("go") ){
					

			ruta = "menuUnidades";
			
			Constantes.getInsumos().clear();
			Constantes.getInsumosList().clear();
			
			
			

		}
		
		
		forward = mapping.findForward(ruta);

		return forward;
	}
	
	public ActionForward salir(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		log.info("LoginAction Metodo: salir ");
		
		String ruta = "login";
		
	    request.getSession().invalidate(); 
		
		ActionForward forward = new ActionForward();
		
		forward = mapping.findForward(ruta);
		
		return forward;
	}
	
}
